<template>
  <div class="post-list-page">
    <div class="container">
      <div class="page-header">
        <h1 class="page-title">所有文章</h1>
        <div class="search-box">
          <input 
            type="text" 
            v-model="searchQuery" 
            placeholder="搜索文章..." 
            class="search-input"
            @input="filterPosts"
          />
        </div>
      </div>
      
      <div class="filters">
        <div class="filter-group">
          <label>分类:</label>
          <select v-model="selectedCategory" @change="filterPosts" class="filter-select">
            <option value="">全部分类</option>
            <option v-for="category in categories" :key="category" :value="category" v-text="category">
            </option>
          </select>
        </div>
        
        <div class="filter-group">
          <label>标签:</label>
          <select v-model="selectedTag" @change="filterPosts" class="filter-select">
            <option value="">全部标签</option>
            <option v-for="tag in tags" :key="tag" :value="tag" v-text="tag">
            </option>
          </select>
        </div>
      </div>
      
      <div class="posts-grid">
        <PostCard 
          v-for="post in filteredPosts" 
          :key="post.id" 
          :post="post"
        />
      </div>
      
      <div v-if="filteredPosts.length === 0" class="no-posts">
        <p>没有找到匹配的文章。</p>
      </div>
    </div>
  </div>
</template>

<script>
import PostCard from '@/components/PostCard.vue'
import { allPosts } from '../utils/mock.js'

export default {
  name: 'PostListPage',
  components: {
    PostCard
  },
  data() {
    return {
      searchQuery: '',
      selectedCategory: '',
      selectedTag: '',
      posts: allPosts,
      filteredPosts: []
    }
  },
  computed: {
    categories() {
      const categories = this.posts.map(post => post.category)
      return [...new Set(categories)]
    },
    tags() {
      const allTags = this.posts.flatMap(post => post.tags)
      return [...new Set(allTags)]
    }
  },
  mounted() {
    this.filteredPosts = [...this.posts]
  },
  methods: {
    formatDate(dateString) {
      const options = { year: 'numeric', month: 'long', day: 'numeric' }
      return new Date(dateString).toLocaleDateString('zh-CN', options)
    },
    filterPosts() {
      this.filteredPosts = this.posts.filter(post => {
        const matchesSearch = post.title.toLowerCase().includes(this.searchQuery.toLowerCase()) || 
                             post.excerpt.toLowerCase().includes(this.searchQuery.toLowerCase()) ||
                             post.tags.some(tag => tag.toLowerCase().includes(this.searchQuery.toLowerCase()))
        
        const matchesCategory = this.selectedCategory ? post.category === this.selectedCategory : true
        
        const matchesTag = this.selectedTag ? post.tags.includes(this.selectedTag) : true
        
        return matchesSearch && matchesCategory && matchesTag
      })
    }
  }
}
</script>

<style scoped>
.post-list-page {
  width: 100%;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 2rem;
  padding: 2rem 0;
  border-bottom: 1px solid #eee;
}

.page-title {
  font-size: 2rem;
  color: #333;
  position: relative;
}

.page-title::after {
  content: '';
  display: block;
  width: 60px;
  height: 4px;
  background: linear-gradient(to right, #667eea, #764ba2);
  margin: 0.5rem 0;
  border-radius: 2px;
}

.search-box {
  flex: 1;
  max-width: 300px;
  margin-left: 2rem;
}

.search-input {
  width: 100%;
  padding: 0.8rem 1rem;
  border: 2px solid #eee;
  border-radius: 30px;
  font-size: 1rem;
  transition: border-color 0.3s;
}

.search-input:focus {
  outline: none;
  border-color: #667eea;
}

.filters {
  display: flex;
  gap: 2rem;
  margin-bottom: 2rem;
  padding: 1.5rem;
  background: white;
  border-radius: 12px;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.05);
}

.filter-group {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.filter-group label {
  font-weight: 600;
  color: #333;
}

.filter-select {
  padding: 0.5rem 1rem;
  border: 2px solid #eee;
  border-radius: 30px;
  background: white;
  font-size: 1rem;
  transition: border-color 0.3s;
}

.filter-select:focus {
  outline: none;
  border-color: #667eea;
}

.posts-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 2rem;
}

.post-card {
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.post-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.15);
}

.post-image img {
  width: 100%;
  height: 200px;
  object-fit: cover;
}

.post-content {
  padding: 1.5rem;
}

.post-meta {
  display: flex;
  justify-content: space-between;
  margin-bottom: 1rem;
  font-size: 0.85rem;
  color: #666;
}

.post-category {
  background: linear-gradient(to right, #667eea, #764ba2);
  color: white;
  padding: 0.2rem 0.8rem;
  border-radius: 20px;
  font-weight: 600;
}

.post-title {
  margin-bottom: 1rem;
  font-size: 1.3rem;
}

.post-title a {
  text-decoration: none;
  color: #333;
  transition: color 0.3s;
}

.post-title a:hover {
  color: #667eea;
}

.post-excerpt {
  color: #666;
  margin-bottom: 1.5rem;
  line-height: 1.6;
}

.post-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.tag {
  background: #f0f2f5;
  color: #666;
  padding: 0.3rem 0.8rem;
  border-radius: 20px;
  font-size: 0.8rem;
  transition: all 0.3s;
}

.tag:hover {
  background: #667eea;
  color: white;
}

.no-posts {
  text-align: center;
  padding: 3rem;
  color: #666;
  font-size: 1.1rem;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .page-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 1rem;
  }
  
  .search-box {
    max-width: 100%;
    margin-left: 0;
    width: 100%;
  }
  
  .filters {
    flex-direction: column;
    gap: 1rem;
  }
  
  .posts-grid {
    grid-template-columns: 1fr;
    gap: 1.5rem;
  }
}
</style>